<?php
require_once __DIR__ . '/FileBuilder.php';

class FileTypeA extends FileBuilder
{
    protected $_wave;
    public $_length;
    public $_size;
    public $_fotmatname;
    public function __construct($desfile = null)
    {
        $this->_wave = $desfile;
    }
    /**
     * 合并wav文件到一个临时文件夹，返回文件路径
     *
     * @param [array] $files 合并文件的数组
     * @return [bool]  
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public function mergerWaves($files)
    {
        if(!isset($this->_wave))
        {
            var_dump($this->_wave);
            throw new \Exception('do not find file');
            return false;
        }
        try {
            //删除原始文件
            //@unlink($this->_wave);
            $cmd = "sox ";
            foreach ($files as $file) {
                $cmd .= $file . " ";
            }
            $cmd .= $this->_wave;
            exec($cmd);
            if (!is_file($this->_wave)) {
                throw new \Exception('merger file do not exsit');
                return false;
            }
            return $this;
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
            //记录日志
            return false;
        }
    }

    /**
     * 将wav文件转化成插件可读的json文件
     *
     * @param [type] $jsonpath json文件路径
     * @return void
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public function wave2json($jsonpath)
    {
        if(!isset($this->_wave))
        {
            return false;
        }
        try {
            $length = ceil($this->_length * 16);
            $cmd = "wav2json $this->_wave -s $length -o " . "/home/wwwroot/default/test.json";
            system($cmd);
            $ret = file_get_contents("/home/wwwroot/default/test.json");
            $data = json_decode($ret);
            foreach ($data->left as $key => $val) {
                $rt[$key] = $val;
            }
            file_put_contents($jsonpath, json_encode($rt));
            if (!is_file($jsonpath)) {
                throw new \Exception('merger create file fail');
                return false;
            }
            return $this;
        } catch (\Exception $e) {
            //return false;
            throw new \Exception($e->getMessage());
        }
    }


    public function wave2mp3($mp3path)
    {
        if(!isset($this->_wave))
        {
            return false;
        }
        try {
            //$cmd = "lame  --quiet -b 32 --resample 11  $this->_wave $mp3path";
            $cmd = "ffmpeg -y -f s16le -ar 16000 -ac 1 -i '$this->_wave' '$mp3path'";
            exec($cmd);
            if (!is_file($mp3path)) {
                throw new \Exception('merger create file fail');
                return false;
            }
            $this->_fotmatname = pathinfo($mp3path)['filename'];
            $this->_size = filesize($mp3path);
            return $this;
        } catch (\Exception $e) {
            //return false;
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 获取wav文件的时长详情
     *
     * @param [type] $wave  文件路径
     * @param [type] $detail  保存文件详情的文件路径
     * @return void
     * @Description
     * @example
     * @author Kelike
     * @since
     */
    public function waveDetailForLength()
    {
        if(!isset($this->_wave))
        {
            return false;
        }
        try {
            $detail = "sox.stat";
            $cmd = "sox $this->_wave -n stat 2> $detail";
            exec($cmd);
            $content = file_get_contents($detail);
            $start_num = strpos($content, "Length (seconds):");
            $end_num = strpos($content, "Scaled by:");
            $this->_length = trim(substr($content,$start_num+17, $end_num-strlen($content)));
            return $this;
        } catch (\Exception $e) {
            return false;
        }
    }

}
